#include "../../common/detail.hpp"
#include "../../common/net.hpp"
#include "../../common/dispatcher.hpp"
#include "../../server/rpc_router.hpp"

void Add(const Json::Value &req, Json::Value &rsp) {
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;
}
int main()
{
    auto dispatcher = std::make_shared<yfrpc::Dispatcher>();
    auto router = std::make_shared<yfrpc::server::RpcRouter>();
    std::unique_ptr<yfrpc::server::SDescribeFactory> desc_factory(new yfrpc::server::SDescribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1", yfrpc::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2", yfrpc::server::VType::INTEGRAL);
    desc_factory->setReturnType(yfrpc::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    router->registerMethod(desc_factory->build());


    auto cb = std::bind(&yfrpc::server::RpcRouter::onRpcRequest, router.get(), std::placeholders::_1, std::placeholders::_2);
    dispatcher->registerHandler<yfrpc::RpcRequest>(yfrpc::MType::REQ_RPC, cb);
    auto server = yfrpc::ServerFactory::create(9090);
    auto message_cb = std::bind(&yfrpc::Dispatcher::onMessage, dispatcher.get(), std::placeholders::_1, std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();
    return 0;
}